<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
            时间：O(N^2)
            空间：O(N)
        */
        function TreeNode(val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
        var a = new TreeNode(3)
        var b = new TreeNode(1)
        var c = new TreeNode(4)
        var d = new TreeNode(null)
        var e = new TreeNode(2)

        a.left = b
        a.right = c
        b.left = d
        b.right = e

        var aa = new TreeNode(1)
        var bb = new TreeNode(null)
        var cc = new TreeNode(2)
        aa.left = bb
        aa.right = cc
        // 中序遍历，把数据放到数组，然后根据k - 1去拿数据
        var kthSmallest = function (root, k) {
            let count = 0;
            let arr = [] // 存取二叉树的所有节点内容
            function dfs (node) {
                if (!node) {
                    return null
                }
                // 左
                dfs(node.left)
                // 中
                if (node.val !== null) {
                    // 节点值不能是null
                    arr.push(node.val)
                }
                // 右
                dfs(node.right)
            }
            dfs(root)
            return arr[k - 1]
        };
        console.log(kthSmallest(a, 1));
        // console.log(kthSmallest(aa, 2));
    </script>
</body>

</html>